package com.hzky.demo.controller;

import com.hzky.demo.common.BackGroundConst;
import com.hzky.demo.dao.HomeworkDao;
import com.hzky.demo.dao.HomeworkUploadDao;
import com.hzky.demo.dao.StudentDao;
import com.hzky.demo.model.Homework;
import com.hzky.demo.model.HomeworkUpload;
import com.hzky.demo.model.Student;
import com.hzky.demo.model.Teacher;
import com.hzky.demo.service.HomeworkService;
import com.hzky.demo.service.StudentService;
import com.hzky.demo.vo.HomeworkStatisticsVo;
import com.hzky.demo.vo.HomeworkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2017/6/8.
 */
@Controller
@Transactional
@RequestMapping(value = "/homework")
public class HomeworkController {

    @Autowired
    HomeworkService homeworkService;
    @Autowired
    StudentService studentService;
    @Autowired
    HomeworkUploadDao homeworkUploadDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    HomeworkDao homeworkDao;

    @RequestMapping(value ="/uploadpage/{studentid}/{homeworkId}" )
    String uploadpage(@PathVariable("studentid")Integer studentId, @PathVariable("homeworkId")Integer homeworkId, Model model)
    {
        Homework homework=homeworkService.getHomeworkbyId(homeworkId);
        Student student=studentService.getStudentById(studentId);
        if(student.getPicture()==null)
            student.setPicture(BackGroundConst.NORMAL_STUDENT_PICTURE);

        if(homework.getDate()==null)
            homework.setDate(new Date());
        model.addAttribute("student",student);
        model.addAttribute("homework",homework);
        return "homeworkUploadPage_33";
    }

    @RequestMapping(value = "/save/{homeworkId}/{studentId}")
    String homeworksave(@PathVariable("homeworkId")Integer homeworkId, @PathVariable("studentId") Integer studentId, @RequestParam("filedata") MultipartFile file, Model model)
    {
        if(file.isEmpty()) return "uploadFailure";


        HomeworkUpload homeworkUpload = homeworkUploadDao.getByHomeworkIdandStudentId(homeworkId,studentId);
        if(homeworkUpload==null)
            homeworkUpload=new HomeworkUpload();
        Homework homework = homeworkService.getHomeworkbyId(homeworkId);
        Student student = studentService.getStudentById(studentId);
        homeworkUpload.setHomework(homework);
        homeworkUpload.setStudent(student);
        try {
            // 文件保存路径
            String temp=file.getOriginalFilename();
            String[] list=temp.split("\\.");
            String fileName=String.valueOf(studentId)+"_"+String.valueOf(homeworkId)+"."+list[list.length-1];
            String filePath= URLDecoder.decode(Class.class .getResource("/").getPath(),"UTF-8") +"static"+BackGroundConst.HOMEWORK_SAVE_PATH+fileName;
            // 转存文件
            file.transferTo(new File(filePath));
            System.out.println(filePath);
            homeworkUpload.setSource(BackGroundConst.HOMEWORK_SAVE_PATH+fileName);
            homeworkUploadDao.save(homeworkUpload);
        } catch (Exception e) {
            e.printStackTrace();
            return "uploadFailure";
        }

        return "uploadSuccess";

    }

    @RequestMapping(value = "/statistics/{homeworkId}")
    String homeworkstatics(@PathVariable("homeworkId")Integer id,Model model)
    {

        Homework homework=homeworkService.getHomeworkbyId(id);
        List<Student> allstudents=studentDao.getStudentsByHomeworkId(id);

        List<Student> finishstudents=studentDao.getFinishStudentsByHomeworkId(id);

        List<Student> unfinishstudents=new ArrayList<Student>();
        for (Student student:allstudents)
            if(!finishstudents.contains(student))
                unfinishstudents.add(student);

        HomeworkStatisticsVo unfinish=new HomeworkStatisticsVo();
        HomeworkStatisticsVo finish=new HomeworkStatisticsVo();
        String allnamelist = new String();
        for (Student student:unfinishstudents)
            allnamelist+=student.getName()+"、";
        unfinish.setNums(allstudents.size());
        unfinish.setNames(allnamelist);
        if (allstudents.isEmpty())
            unfinish.setNames("没有学生");


        String finishnamelist=new String();
        for (Student student:finishstudents)
            finishnamelist+=student.getName()+"、";
        finish.setNames(finishnamelist);
        finish.setNums(finishstudents.size());
        if(finishstudents.isEmpty())
            finish.setNames("没有学生");

        model.addAttribute("unfinish",unfinish);
        model.addAttribute("finish",finish);
        model.addAttribute("homework",homework);
        return "homeworkStatics_31";
    }
    @RequestMapping(value ="/homeworklist/{studentid}" )
    String homeworklist(@PathVariable("studentid")Integer studentId, Model model)
    {
        List<HomeworkVo> homeworkVos=new ArrayList<HomeworkVo>();
        List<Homework> homeworks=homeworkDao.getHomeworkByStudentId(studentId);
        for(Homework homework:homeworks)
        {
            HomeworkVo homeworkVo=new HomeworkVo();
            homeworkVo.setHomeworkName(homework.getName());
            homeworkVo.setInformation(homework.getInformation());
            Teacher teacher=homework.getCourse().getTeacher();
            homeworkVo.setTeacherName(teacher.getName());
            if(teacher.getPicture()!=null)
                homeworkVo.setTeacherPicture(teacher.getPicture());
            else
                homeworkVo.setTeacherPicture(BackGroundConst.NORMAL_TEACHER_PICTURE);
            homeworkVos.add(homeworkVo);
        }
        model.addAttribute("homeworks",homeworkVos);

        return "homeworklist_21";
    }



}
